package paf.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import paf.model.Class;
import paf.model.DiagramExpressionModel;
import paf.model.Observable;
import paf.model.UseCase;

public class ConnectionsUI extends JPanel implements Observer, ActionListener {
	
	private JLabel lblConnections, lblDelimiter;
	private JComboBox cmbClasses, cmbUseCases;
	private JCheckBox chkCreate, chkRead, chkUpdate, chkDelete;
	private JButton btnSave;
	private JPanel pnlCheckboxes;
	private DiagramExpressionModel model;
	
	protected ConnectionsUI() {
		super(new BorderLayout());
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		this.model = DiagramExpressionModel.get();
		model.addObserver(this);
		
		lblConnections = new JLabel("Connect the class with a use case");
		
		cmbClasses = new JComboBox();
		cmbClasses.addItem("Class");
		
		lblDelimiter = new JLabel("-");
		
		cmbUseCases = new JComboBox();
		cmbUseCases.addItem("Usecase");
		
		update(model);
		
		pnlCheckboxes = new JPanel(new GridLayout(2, 2));
		
		chkCreate = new JCheckBox("Create");
		chkRead = new JCheckBox("Read");
		chkUpdate = new JCheckBox("Update");
		chkDelete = new JCheckBox("Delete");
		
		btnSave = new JButton("Save");
		btnSave.setAlignmentX(Component.LEFT_ALIGNMENT);
		btnSave.addActionListener(this);
		
		renderView();
	}
	
	public void renderView() {
		JPanel pnlConnection = new JPanel(new FlowLayout(FlowLayout.LEFT));
		pnlConnection.add(lblConnections);
		this.add(pnlConnection);
		JPanel pnlConnect = new JPanel(new FlowLayout(FlowLayout.LEFT));
			pnlConnect.add(cmbClasses);
			pnlConnect.add(lblDelimiter);
			pnlConnect.add(cmbUseCases);
		this.add(pnlConnect);
			pnlCheckboxes.add(chkCreate);
			pnlCheckboxes.add(chkRead);
			pnlCheckboxes.add(chkUpdate);
			pnlCheckboxes.add(chkDelete);
		this.addLeft(pnlCheckboxes);
		this.addLeft(btnSave);
	}

	private void addLeft(Component c) {
		JPanel pnlContainer = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
		pnlContainer.add(c);
		this.add(pnlContainer);
	}
	
	@Override
	public void update(Observable o) {
		System.out.println("Update ConnectionsUI");
		cmbClasses.removeAllItems();
		cmbClasses.addItem("Class");
		for(Class c : model.getClasses()) {
			cmbClasses.addItem(c);
		}
		cmbUseCases.removeAllItems();
		cmbUseCases.addItem("Use Case");
		for(UseCase u : model.getUseCases()) {
			cmbUseCases.addItem(u);
		}
	}

	public void resetView() {
		cmbClasses.setSelectedIndex(0);
		cmbUseCases.setSelectedIndex(0);
		chkCreate.setSelected(false);
		chkRead.setSelected(false);
		chkUpdate.setSelected(false);
		chkDelete.setSelected(false);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave) {
			if(cmbClasses.getSelectedItem() instanceof Class &&
					cmbUseCases.getSelectedItem() instanceof UseCase) {
				model.createConnection((Class) cmbClasses.getSelectedItem(), 
						(UseCase) cmbUseCases.getSelectedItem(),
						chkCreate.isSelected(),
						chkRead.isSelected(),
						chkUpdate.isSelected(),
						chkDelete.isSelected());
				resetView();
			}
		}
	}

}
